package com.yh.crystal;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

public class AppInit {

	private static final cn.hutool.log.Log log = cn.hutool.log.LogFactory.get();

	private static final Map<String, Map<String, Object>> param = MapUtil.newHashMap(true);

	private static final String lx = Base64.decodeStr("57G75Z6L"); // 类型
	private static final String zwmc = Base64.decodeStr("5Lit5paH5ZCN56ew");// 中文名称
	private static final String ywmc = Base64.decodeStr("6Iux5paH5ZCN56ew");// 英文名称

	private static final String crystal = "crystal.xlsx";

	private AppInit() {
	}

	public static Map<String, Map<String, Object>> getParam() {
		Map<String, Map<String, Object>> p = MapUtil.newHashMap(true);
		p.putAll(param);
		return p;
	}

	public static synchronized void init() {
		param.clear();

		File excel = FileUtil.file(crystal);
		log.info(excel.getAbsolutePath());
		ExcelReader reader = ExcelUtil.getReader(excel, "all");
		List<Map<String, Object>> all = reader.readAll();

		List<Object> sheets = all.stream().map((row) -> (row.get("sheet"))).collect(Collectors.toList()); // 获取sheet名称
		sheets.stream().forEach((s) -> {
			String name = s.toString();
			List<List<Object>> rows = ExcelUtil.getReader(excel, name).read();
			Map<String, Object> one = build(name, rows); // 构建每一个请求全部
			param.put(name, one);
		});
		log.info("init success");

		reader.close();
	}

	public static void main(String[] args) {
		AppInit.init();
	}

	private static Map<String, Object> build(String name, List<List<Object>> all) {
		List<Object> types = all.stream()
				.filter((n) -> (lx.equals(n.get(0)) && zwmc.equals(n.get(1)) && ywmc.equals(n.get(2)))).findFirst()
				.get();
		List<List<Object>> system = all.stream().filter((n) -> ("system".equals(n.get(0))))
				.collect(Collectors.toList());
		List<List<Object>> request = all.stream().filter((n) -> ("request".equals(n.get(0))))
				.collect(Collectors.toList());
		List<List<Object>> response = all.stream().filter((n) -> ("response".equals(n.get(0))))
				.collect(Collectors.toList());

		Map<String, Object> systemMap = build_systemMap(system); // 构建system参数
		List<Map<String, Object>> requestMap = build_requestMap(types, request); // 构建request参数
		List<Map<String, Object>> responseMap = build_responseMap(types, response);// 构建response参数

		Map<String, Object> map = MapUtil.newHashMap(true);
		map.put("name", name);
		map.put("system", systemMap);
		map.put("request", requestMap);
		map.put("response", responseMap);
		log.info("init {}, system={}, request={}, response={}", name, systemMap.size(), requestMap.size(),
				responseMap.size());
		return map;

	}

	private static Map<String, Object> build_systemMap(List<List<Object>> systemlist) {
		Map<String, Object> map = MapUtil.newHashMap(true);
		systemlist.stream().forEach(n -> {
			map.put((String) n.get(1), (String) n.get(2));
		});
		return map;
	}

	private static List<Map<String, Object>> build_requestMap(List<Object> types, List<List<Object>> request) {
		return build_responseMap(types, request);
	}

	private static List<Map<String, Object>> build_responseMap(List<Object> types, List<List<Object>> responselist) {
		List<Map<String, Object>> list = ListUtil.list(true);
		responselist.stream().forEach(n -> {
			Map<String, Object> map = MapUtil.newHashMap(true);
			map.put((String) n.get(2), toMap(types, n));
			list.add(map);
		});
		return list;
	}

	private static Object toMap(List<Object> types, List<Object> n) {
		Map<String, String> map = MapUtil.newHashMap(true);
		for (int i = 0; i < n.size(); i++) {
			map.put(types.get(i).toString(), n.get(i).toString());
		}
		return map;
	}

}
